package edu.zut.cs.software.perfect.attendance.service;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.perfect.attendance.domain.CourseAttendance;
import edu.zut.cs.software.perfect.base.service.GenericGenerator;




public class AttendanceEntityGeneratorOne extends GenericGenerator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(AttendanceEntityGeneratorOne.class.getName());

	List<CourseAttendance> courseAttendanceList;

	CourseAttendanceManager courseAttendanceManager;
	@Autowired
	public void setCourseAttendanceManager(CourseAttendanceManager courseAttendanceManager) {
		this.courseAttendanceManager = courseAttendanceManager;
	}

	@Before
	public void setUp() throws Exception {
		this.courseAttendanceList = new ArrayList<CourseAttendance>();
		InputStream input = CourseAttendanceManagerTest.class.getResourceAsStream("/courseAttendance.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			CourseAttendance s = new CourseAttendance();
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setCourseCode(value);
					if (j == 2)
						s.setCourseName(value);
					if (j == 3)
						s.setClassId(value);
					if (j == 4)
						s.setStudentId(value);
					if (j == 5)
						s.setStudentName(value);
					if (j == 6)
						s.setWhetherAttendance(value);
					if (j == 7)
						s.setAttendanceRate(value);
				}
			}
			this.courseAttendanceList.add(s);
		}
	}

	@Test
	public void test() {
		this.courseAttendanceManager.deleteAll();
		this.courseAttendanceManager.save(this.courseAttendanceList);
		Iterable<CourseAttendance> result = this.courseAttendanceManager.findAll();

		if (logger.isInfoEnabled()) {
			logger.info("test() - Iterable<CourseAttendance> result={}", result); //$NON-NLS-1$
		}
	}

}
